package com.itheima.Controller;


import com.itheima.Context.BaseContext;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/validatecode")
@Slf4j
@Api(tags ="短信认证")
public class ValidatecodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send4Order")
    @ApiOperation("发送短信验证码")
    public Result send4OrderPOST(@RequestParam String telephone){
        log.info("发送短信验证码：{}",telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        log.info("验证码：{}",validateCode);
        redisTemplate.opsForHash().put("验证码",telephone,validateCode.toString());


        return new Result(true,"验证码发送成功",null);
    }


    @GetMapping("/send4Order")
    @ApiOperation("发送短信验证码")
    public Result send4OrderGET(@RequestParam String telephone){
        log.info("发送短信验证码：{}",telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        log.info("验证码：{}",validateCode);
        redisTemplate.opsForHash().put("验证码",telephone,validateCode.toString());


        return new Result(true,"验证码发送成功",null);
    }



}
